var PATH_PREFIX = "";
function setPathPrefix(path){
	PATH_PREFIX =  path;
}

$(function(){
	
	// select2控件初始化
	$(".select2").select2();
	
	// 初始化时间控件
	WASP_WIDGET.triggerDateStyleWithYMD("#assessmentDate");
	// 招商财富产品线产品
	WASP_WIDGET.triggerPrdLineSelect("subPrdCode");
	//回显招商财富产品线产品的值
	WASP_WIDGET.initializeSelectVal('subPrdCode');
	
	$("#addTreNavForm").validate({
		focusCleanup : true,
			   rules :{
				   'subPrdCode':{
					   required : true
				   },
				   'assessmentDate':{
						   required : true
				   },
				   'nav':{
					   required : true,
					   number:true,                
		               min:0 
				   },
				   'totalNav':{
					   /*required : true,*/
					   number:true,                
		               min:0 
				   },
				   'prdNavSrc':{
					   required : true
				   },
				   'quotient':{
					   /*required : true,*/
					   number:true,                
		               min:0 
				   },
				   'totalValue':{
					  /* required : true,*/
					   number:true,                
		               min:0 
				   },
				   'totalAsset':{
					   /*required : true,*/
					   number:true,                
		               min:0 
				   }
			   },
			   messages :{
				   'subPrdCode' : {
						required : "产品不能为空"
					},
					'assessmentDate' : {
						required : "估值日期不能为空"
					},
					'nav' : {
						required : "单位净值不能为空",
						number : '请输入合法的数字',                
		                min : '请输入大于零的数字' 
					},
					'totalNav' : {
						/*required : "累计单位净值不能为空",*/
						number : '请输入合法的数字',                
		                min : '请输入大于零的数字'
					},
					'prdNavSrc' : {
						required : "净值来源不能为空"
					},
					'quotient' : {
						/*required : "份额不能为空",*/
						number : '请输入合法的数字',                
		                min : '请输入大于零的数字'
					},
					'totalValue' : {
						/*required : "总净值不能为空",*/
						number : '请输入合法的数字',                
		                min : '请输入大于零的数字'
					},
					'totalAsset' : {
						/*required : "总资产不能为空",*/
						number : '请输入合法的数字',                
		                min : '请输入大于零的数字'
					}
			   }
	});
});

function isRepeat(element){
	debugger;
	//验证是否重复，pass为false校验不通过
	var pass = true;
	var navBizId = $('#navBizId').val();
	var assessmentDate = $("#assessmentDate").val();
	var subPrdCode = $("#subPrdCode").val();
	if(navBizId == null && navBizId == ''){
		navBizId = null;
	}
	if(!assessmentDate)
		return true;
	
	$.ajax({
		  url: PATH_PREFIX+"isRepeat.do",
		  dataType: "json",
		  type: "POST",
		  cache: false,
		  async: false,
		  data: {
			  navBizId : navBizId,
			  subPrdCode : subPrdCode,
			  assessmentDate : assessmentDate
		  },
		  success: function( data ) {
				if(data){
					pass=data.success;
				}
		  },
		  error: function(jqXHR, textStatus, errorThrown){
				ctools.alert("查询失败" + textStatus);
				pass = false;
		  }
	});
	return pass;
};


// 表单提交
function submitForm(){
	
	SubmitAndPreventSecond("addTreNavForm",function() {
		debugger;
		if(!$("form[name='addTreNavForm']").valid()) {
			toastr.warning('', '请完善信息');
			return false;
		}
		 var result = isRepeat($('#assessmentDate'));
		if(!result) {
			toastr.warning('', $('#subPrdCode').text()+'估值日:'+$('#assessmentDate').val()+"已有数据");
			return false;
		}
		var navBizId = $('#navBizId').val();
		var url = PATH_PREFIX + "saveTreNav.do";
		if(navBizId && navBizId.length > 0){
			url = PATH_PREFIX + "updateTreNav.do";
		}
		$('#addTreNavForm').attr('action', url);
		
		return true;
	}, true);
}





